<template>
    <div>
        <div style="margin-top: 10px; display: flex;justify-content: center">
            <el-input v-model="keywords" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search"
                      style="width: 400px;margin-right: 10px" @keydown.enter.native="doSearch"/>
            <el-button icon="el-icon-search" type="primary" round @click="doSearch">搜索</el-button>
        </div>
        <div class="hr-container">
            <el-card class="hr-card" v-for="(hr,index) in hrs" :key="index">
                <div slot="header" class="clearfix">
                    <span>{{hr.name}}</span>
                    <el-button style="float: right; padding: 3px 0;color: red" type="text"
                               icon="el-icon-delete"  @click="deleteHr(hr)"></el-button>
                </div>
                <div>
                    <div class="img-container">
                        <img :src="hr.userface" :alt="hr.name" :title="hr.name" class="userface-img">
                    </div>
                    <div class="userinfo-container">
                        <div>用户名：{{hr.name}}</div>
                        <div>手机号码：{{hr.phone}}</div>
                        <div>电话号码：{{hr.telephone}}</div>
                        <div>地址：{{hr.address}}</div>
                        <div>用户状态：
                            <el-switch
                                    v-model="hr.enabled"
                                    active-text="启用"
                                    @change="enabledChange(hr)"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    inactive-text="禁用">
                            </el-switch>
                        </div>
                        <div>用户角色：
                            <el-tag type="success" style="margin-right: 4px" v-for="(role,indexj) in hr.roles"
                                    :key="indexj">{{role.nameZh}}
                            </el-tag>
                            <el-popover
                                    placement="bottom"
                                    title="角色列表"
                                    width="200"
                                    @show="showPop(hr)"
                                    @hide="hidePop(hr)"
                                    trigger="click">
                                <!--@hide 隐藏之后执行添加新角色的url (此过程先删除后先添加)-->
                                <el-select v-model="selectedRoles" multiple placeholder="请选择">
                                    <!--multiple多选,selectedRoles默认选择的角色及后续添加的角色-->
                                    <el-option
                                            v-for="(r,indexk) in allRoles"
                                            :key="indexk"
                                            :label="r.nameZh"
                                            :value="r.id">  <!--后续选择添加的角色，用id进行提交-->
                                    </el-option>
                                </el-select>
                                <el-button slot="reference" icon="el-icon-more" type="text"></el-button>
                            </el-popover>
                        </div>
                        <div>备注：{{hr.remark}}</div>
                    </div>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
    export default {
        name: "SysHr",
        data() {
            return {
                keywords: '',
                hrs: [],
                allRoles: [],
                selectedRoles: [],
            }
        },
        mounted() {
            this.initHrs();
        },
        methods: {
            deleteHr(hr) {
                this.$confirm('此操作将永久删除【'+hr.name+'】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/hr/"+hr.id).then(resp=>{
                        if (resp) {
                            this.initHrs();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doSearch(){
              this.initHrs();
            },
            initHrs() {
                this.getRequest("/system/hr/?keywords="+this.keywords).then(resp => {
                    if (resp) {
                        this.hrs = resp;
                    }
                })
            },
            enabledChange(hr) {
                delete hr.roles;
                this.putRequest("/system/hr/", hr).then(resp => {
                    if (resp) {
                        this.initHrs();
                    }
                })
            },
            hidePop(hr) {
                /*用于修改没有添加数据仍然执行更新的操作*/
                let roles = [];
                Object.assign(roles, hr.roles);  /*深拷贝一份 防止后续比较清楚的时候把展示的数据清除掉*/
                let flag = false;
                /*首先判断原始数据和selectedRoles中存储的数据长度是否相等*/
                if (roles.length !== this.selectedRoles.length) {
                    flag = true;
                } else {
                    /*如果两者数据长度相等,则比较数据id，roles是对象，selectedRoles是id，所以在这边循环比较*/
                    for (let i = 0; i < roles.length; i++) {
                        let role = roles[i];
                        for (let j = 0; j < this.selectedRoles.length; j++) {
                            let sr = this.selectedRoles[j];
                            if (role.id === sr) {
                                /*有相等的就清除掉*/
                                roles.splice(i, 1);
                                /*当清除的时候，2就会占据1的位置，同时会默认进行i++,从而会直接查看到3，略过数字2，所以这里要i--*/
                                i--;
                                break;
                            }
                        }
                    }
                    if (roles.length !== 0) {
                        /*不等于0相当于不完全相等，就需要更新了*/
                        flag = true;
                    }
                }
                if (flag) {
                    let url = '/system/hr/role?hrid=' + hr.id;
                    this.selectedRoles.forEach(sr => {  /*selectedRoles返回的是每个角色的id,正好对应遍历的rids*/
                        url += '&rids=' + sr;
                    });
                    this.putRequest(url).then(resp => {
                        if (resp) {
                            this.initHrs();
                        }
                    });
                }
            },
            showPop(hr) {
                this.initAllRoles();
                /*将默认选择的角色添加进selectedRoles中*/
                let roles = hr.roles;
                this.selectedRoles = [];
                roles.forEach(r => {
                    this.selectedRoles.push(r.id);
                })
            },
            initAllRoles() {
                this.getRequest("/system/hr/roles").then(resp => {
                    if (resp) {
                        this.allRoles = resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .hr-container {
        margin-top: 10px;
        display: flex; /*水平排列*/
        flex-wrap: wrap; /*自动换行*/
        justify-content: space-around; /*间隔扩大*/
    }

    .hr-card {
        width: 350px;
        margin-bottom: 20px;
    }

    .userinfo-container div {
        font-size: 12px;
        color: #409eff;
    }

    .userinfo-container {
        margin-top: 20px;
    }

    .img-container {
        width: 100%;
        display: flex;
        justify-content: center;
    }

    .userface-img {
        width: 72px;
        height: 72px;
        border-radius: 72px;
    }
</style>
